package com.ruoyi.game.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.game.domain.GamePermission;
import com.ruoyi.game.service.IGamePermissionService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 权限Controller
 *
 * @author qidian
 * @date 2022-05-06
 */
@RestController
@RequestMapping("/permission")
public class GamePermissionController extends BaseController
{
    @Autowired
    private IGamePermissionService gamePermissionService;

    /**
     * 查询权限列表
     */
    @RequiresPermissions("game:permission:list")
    @GetMapping("/list")
    public TableDataInfo list(GamePermission gamePermission)
    {
        startPage();
        List<GamePermission> list = gamePermissionService.selectGamePermissionList(gamePermission);
        return getDataTable(list);
    }

    /**
     * 导出权限列表
     */
    @RequiresPermissions("game:permission:export")
    @Log(title = "权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GamePermission gamePermission)
    {
        List<GamePermission> list = gamePermissionService.selectGamePermissionList(gamePermission);
        ExcelUtil<GamePermission> util = new ExcelUtil<GamePermission>(GamePermission.class);
        util.exportExcel(response, list, "权限数据");
    }

    /**
     * 获取权限详细信息
     */
    @RequiresPermissions("game:permission:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gamePermissionService.selectGamePermissionById(id));
    }

    /**
     * 新增权限
     */
    @RequiresPermissions("game:permission:add")
    @Log(title = "权限", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GamePermission gamePermission)
    {
        return toAjax(gamePermissionService.insertGamePermission(gamePermission));
    }

    /**
     * 修改权限
     */
    @RequiresPermissions("game:permission:edit")
    @Log(title = "权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GamePermission gamePermission)
    {
        return toAjax(gamePermissionService.updateGamePermission(gamePermission));
    }

    /**
     * 删除权限
     */
    @RequiresPermissions("game:permission:remove")
    @Log(title = "权限", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gamePermissionService.deleteGamePermissionByIds(ids));
    }

    /**
     * 升级vip
     *
     * @param gameId    游戏id
     * @param userId    用户id
     * @return  结果
     */
    @PutMapping({"/upgrade/vip/{gameId}", "/upgrade/vip/{gameId}/{userId}"})
    @Log(title = "权限", businessType = BusinessType.UPDATE)
    public AjaxResult upgradeVip(@PathVariable Long gameId, @PathVariable(required = false) Long userId)
    {
        return toAjax(gamePermissionService.upgradeVip(gameId, userId));
    }

    /**
     * 解绑账号
     *
     * @param gameId    游戏id
     * @param userId    用户id
     * @return  结果
     */
    @PutMapping({"/unbound/account/{gameId}", "/unbound/account/{gameId}/{userId}"})
    @Log(title = "权限", businessType = BusinessType.UPDATE)
    public AjaxResult unboundAccount(@PathVariable Long gameId, @PathVariable(required = false) Long userId)
    {
        return toAjax(gamePermissionService.unboundAccount(gameId, userId));
    }
}
